package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.FriendService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加联系人
     * @param loginUserId 登录用户id
     * @param friendId 朋友id
     */
    @Override
    public void addContact(Long loginUserId, Long friendId) {
        Query query1 = new Query(Criteria.where("userId").is(loginUserId)
                                .and("friendId").is(friendId));
        Friend friend = mongoTemplate.findOne(query1, Friend.class);
        if (friend == null) {
            friend = new Friend();
            friend.setUserId(loginUserId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }

        Query query2 = new Query(Criteria.where("userId").is(friendId)
                                .and("friendId").is(loginUserId));
        Friend friend1 = mongoTemplate.findOne(query2, Friend.class);
        if (friend1 == null) {
            friend1 = new Friend();
            friend1.setUserId(friendId);
            friend1.setFriendId(loginUserId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }
    }

    //查找好友列表
    @Override
    public PageBeanVo findContactList(Integer page, Integer pageSize, Long loginUserId) {

        Query query = new Query(Criteria.where("userId").is(loginUserId));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        if (!CollectionUtil.isNotEmpty(friendList)) {
            return null;
        }
        long count = mongoTemplate.count(query, Friend.class);
        return new PageBeanVo(page, pageSize, count, friendList);
    }

    /**
     * 删除好友关系
     * @param loginUserId 登陆用户id
     * @param unLoveId 不喜欢用户id
     */
    @Override
    public void deleteFriend(Long loginUserId, Long unLoveId) {
        Query query1 = new Query(Criteria.where("userId").is(loginUserId)
                                .and("friendId").is(unLoveId));
        Friend friend = mongoTemplate.findOne(query1, Friend.class);
        if (friend != null) {
            mongoTemplate.remove(friend);
        }
        Query query2 = new Query(Criteria.where("userId").is(unLoveId)
                .and("friendId").is(loginUserId));
        Friend friend1 = mongoTemplate.findOne(query2, Friend.class);
        if (friend1 != null) {
            mongoTemplate.remove(friend1);
        }
    }
}
